package com.tony.demo.utils;

import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.widget.Scroller;

import com.puhui.lib.utils.DMLog;

import java.lang.reflect.Field;

/**
 * Copyright:
 * Created by TangJian on 2019/4/20.
 * Description:
 * Modified:
 */
@SuppressWarnings("all")
public class CircleViewPagerHelper {
    private ViewPager viewPager;
    private CountDownTimer countDownTimer;

    private CircleViewPagerHelper(ViewPager viewPager) {
        this.viewPager = viewPager;
        ViewPagerScroller scroller = new ViewPagerScroller(this.viewPager.getContext());
        scroller.setScrollDuration(1000);
        scroller.initViewPagerScroll(this.viewPager); //这个是设置切换过渡时间为1000毫秒
    }

    public static CircleViewPagerHelper getHelper(ViewPager viewPager) {
        return new CircleViewPagerHelper(viewPager);
    }

    public void startCircle() {
        PagerAdapter adapter = viewPager.getAdapter();
        if (adapter == null || adapter.getCount() <= 1) {
            return;
        }

        int millisInFuture = Integer.MAX_VALUE - Integer.MAX_VALUE % 3000;
        int countDownInterval = 3000;
        if (countDownTimer == null) {
            countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    PagerAdapter adapter = viewPager.getAdapter();
                    int nextItemIndex = (viewPager.getCurrentItem() + 1) % adapter.getCount();
                    viewPager.setCurrentItem(nextItemIndex);
                }

                @Override
                public void onFinish() {
                    DMLog.e(this.getClass().getSimpleName(), "onFinish");
                }
            };
        }

        countDownTimer.cancel();
        countDownTimer.start(countDownInterval);

//        new Handler() {
//            @Override
//            public void handleMessage(Message msg) {
//                countDownTimer.start();
//            }
//        }.sendEmptyMessageDelayed(0, 3000);
    }

    public void stopCircle() {
        if (countDownTimer != null && countDownTimer.isRunning()) {
            countDownTimer.cancel();
        }
    }


    /**
     * 这个是设置viewPager切换过度时间的类
     * *        ViewPagerScroller scroller = new ViewPagerScroller(context);
     * *        scroller.setScrollDuration(0);
     * *        scroller.initViewPagerScroll(viewPager);  这个是设置切换过渡时间为0毫秒
     * <p>
     * *        ViewPagerScroller scroller = new ViewPagerScroller(context);
     * *        scroller.setScrollDuration(2000);
     * *        scroller.initViewPagerScroll(viewPager);这个是设置切换过渡时间为2秒
     **/
    public class ViewPagerScroller extends Scroller {
        private int mScrollDuration = 2000;  //滑动速度

        /**
         * 设置速度速度
         *
         * @param duration 时长
         */
        public void setScrollDuration(int duration) {
            this.mScrollDuration = duration;
        }

        public ViewPagerScroller(Context context) {
            super(context);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, mScrollDuration);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy) {
            super.startScroll(startX, startY, dx, dy, mScrollDuration);
        }

        public void initViewPagerScroll(ViewPager viewPager) {
            try {
                Field mScroller = ViewPager.class.getDeclaredField("mScroller");
                mScroller.setAccessible(true);
                mScroller.set(viewPager, this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
